package inc.gome.dynamic.datasource.utils;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 源码参考===>
 * org.springframework.util.PropertyPlaceholderHelper#
 * parseStringValue(java.lang.String,
 * org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver,
 * java.util.Set)
 *
 * @author sujizhou
 * @Date 2021/12/14
 * @Desc 文件夹解析工具类
 */
public class FolderPathUtils {


    /**
     * /job/test-view/job/file_1/
     */

    private static final String placeholderPrefix = "${";

    private static final String placeholderSuffix = "}";

    private static final String simplePrefix = "{";

    /**
     * 一层文件夹名称解析
     */
    private static final String FOLDER_PATH_TEMPLATE = "job/${appname}/job/${1}/job/${2}/job";

    public  static String getFolderPath(String ... args) {
        if(args!=null) {
            List<String> list = Arrays.asList(args);
            String folderPath = parseStringValue(FOLDER_PATH_TEMPLATE, list, 0);
            return folderPath;
        }
        return  null;
    }


/*    public static void main(String[] args) {
        ArrayList list=new ArrayList();
        list.add("test_view");
        list.add("v1");

        String s = parseStringValue(FOLDER_PATH_TEMPLATE, list,0);

        System.out.println(s);

    }*/


    /**
     * 模板解析
     * @param value  要替换的模板
     * @param args  替换参数
     * @return
     */
    private   static  String parseStringValue(
            String value,
            @Nullable List<String> args,int index) {

        int startIndex = value.indexOf(placeholderPrefix);
        if (startIndex == -1) {
            return value;
        }
        StringBuilder result = new StringBuilder(value);
        while (startIndex != -1) {
            int endIndex = findPlaceholderEndIndex(result, startIndex);
            if (endIndex != -1) {
                String placeholder = result.substring(startIndex + placeholderPrefix.length(), endIndex);
                // 递归处理是否还有${}  考虑是否加上
                //parseStringValue(placeholder, args, index);
                // 值动态获取  value
                if(index<args.size()) {
                    String propVal = args.get(index++);
                    result.replace(startIndex, endIndex + placeholderSuffix.length(), propVal);
                    startIndex = result.indexOf(placeholderPrefix, startIndex + propVal.length());
                }else{
                    System.out.println(result.toString());
                    result.replace(startIndex-4,result.length(),"");
                    return result.toString();
                }
            } else {
                startIndex = -1;
            }
        }
        return result.toString();
    }


    //${appp{}}  //解析完整的${}
    private  static int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
        int index = startIndex + placeholderPrefix.length();
        int withinNestedPlaceholder = 0;
        while (index < buf.length()) {
            if (StringUtils.substringMatch(buf, index,placeholderSuffix)) {
                if (withinNestedPlaceholder > 0) {
                    withinNestedPlaceholder--;
                    index = index + placeholderSuffix.length();
                } else {
                    return index;
                }
            } else if (StringUtils.substringMatch(buf, index, simplePrefix)) {
                withinNestedPlaceholder++;
                index = index + simplePrefix.length();
            } else {
                index++;
            }
        }
        return -1;
    }


}
